﻿using ICMS.PluginInterfaces;
using commonImportPlugin.Factorys;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using commonImportPlugin.Config;
using commonImportPlugin.Common;
using commonImportPlugin.Configs.SsseFile;

namespace commonImportPlugin.FilesCore.SsseFile
{
    /// <summary>
    /// 《excel模式通用社保数据解析核心类》
    /// 该模式支持所有excel格式社保数据
    /// 的解析，要求excel格式的数据第一行
    /// 为项名称。第2行开始才为数据
    /// </summary>
    public class SsseFileCoreExcelClass : SsseFileCore
    {
        SsseBaseExcel ssseBase = new SsseBaseExcel();
        static SsseFileConfigExcel config = null;
        CommonUtil commonUtil = CommonUtil.GetInstance();
        static Context context;
        public SsseFileCoreExcelClass GetInstance()
        {
            if (isGetConfigSucceed())
            {
                context = Context.GetConfig();
                return new SsseFileCoreExcelClass();
            }
            return null;
        }
        private bool isGetConfigSucceed()
        {
            config = SsseFileConfigExcel.GetConfig();
            if (config == null)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 获取所有社保数据
        /// </summary>
        /// <param name="ssseFilePath">社保文件路径</param>
        /// <param name="ssseCounts">成功获取到的社保数据量</param>
        /// <returns>社保数据</returns>
        public override List<ICData> GetSsseAllDatas(string ssseFilePath, out int ssseCounts)
        {
            List<ICData> allSsseIcDatas=new List<ICData>();
            List<ICData> finalDatas = new List<ICData>(); 
            allSsseIcDatas=ssseBase.getSsseAllDatas(ssseFilePath, config);
            //过滤数据
            #region 过滤
            if (!string.IsNullOrEmpty(config.filterItem))
            {
                finalDatas = commonUtil.FilterDatasWithFile(allSsseIcDatas, context.succeedLogFilePth, config.filterItem);
            }
            else
                finalDatas = allSsseIcDatas;
            #endregion

            ssseCounts = finalDatas.Count;
            return finalDatas;
        }

        public override bool CheckSsseData(string ssseFilePath, string regexstr)
        {
            throw new NotImplementedException();
        }

        public override int GetSsseAllCounts(string ssseFilePath)
        {
            DataSet originSsseDatas = null;
            originSsseDatas = ssseBase.excelToDataSet(ssseFilePath);
            //int rows = originSsseDatas.Tables[0].Rows.Count;//excelToDataSet有时空白行也会读进固该方式有时会有问题
            int counts = 0;
            foreach (DataRow dr in originSsseDatas.Tables[0].Rows)
            {
                bool isNull = true;
                foreach (DataColumn dataColumn in originSsseDatas.Tables[0].Columns)
                {
                    string data = dr[dataColumn].ToString();
                    if (!string.IsNullOrEmpty(data))
                    {
                        isNull = false;
                        break;
                    }                       
                }
                if (isNull)
                    break;

                counts++;
            }

            
            return counts;
        }
    }
    /// <summary>
    /// 《普通模式通用社保数据解析核心类》
    /// 该模式支持社保数据以行为单位存放，
    /// 每行以特定分隔符分割。该模式支持70%
    /// 以上社保数据。
    /// </summary>
    public class SsseFileCoreClass : SsseFileCore
    {
        SsseBaseGerneral ssseBase = new SsseBaseGerneral();
        static SsseFileConfig config = null;
        CommonUtil commonUtil = CommonUtil.GetInstance();
        static Context context;
        public SsseFileCoreClass GetInstance()
        {
            if (IsGetConfigSucceed())
            {
                context = Context.GetConfig();
                return new SsseFileCoreClass();
            }
            return null;
        }
        private bool IsGetConfigSucceed()
        {
            config = SsseFileConfig.GetConfig();
            if (config == null)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 获取社保数据
        /// </summary>
        /// <param name="ssseFilePath">社保文件路径</param>
        /// <param name="ssseCounts">成功获取到的社保数据量</param>
        /// <returns>社保数据</returns>
        public override List<ICData> GetSsseAllDatas(string ssseFilePath, out int ssseCounts)
        {
            List<ICData> allSsseDatas = new List<ICData>();
            List<ICData> finalDatas  = new List<ICData>();
            allSsseDatas=ssseBase.getSsseAllDatas(ssseFilePath, config);

            ////////////////过滤数据////////////////
            if (!string.IsNullOrEmpty(config.filterItem))
            {
                finalDatas = commonUtil.FilterDatasWithFile(allSsseDatas, context.succeedLogFilePth, config.filterItem);
            }
            else
                finalDatas = allSsseDatas;

            ssseCounts = finalDatas.Count;
            return finalDatas;
        }
        /// <summary>
        /// 检查社保数据的正确性
        /// </summary>
        /// <param name="ssseFilePath"></param>
        /// <param name="regexstr"></param>
        /// <returns></returns>
        public override bool CheckSsseData(string ssseFilePath, string regexstr)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取社保数据量
        /// </summary>
        /// <param name="ssseFilePath">社保文件</param>
        /// <returns>社保数据量</returns>
        public override int GetSsseAllCounts(string ssseFilePath)
        {
            if (!File.Exists(ssseFilePath))
                throw new Exception("社保文件不存在！");
            string[] originSsseLines = File.ReadAllLines(ssseFilePath);
            return originSsseLines.Length - config.dataIndex + 1; ;
        }
    }
}
